197. Rising Temperature


Posted by ikl258794613 on 2024-02-22

Column Name Type
id int
recordDate date
temperature int

id is the column with unique values for this table.
There are no different rows with the same recordDate.
This table contains information about the temperature on a certain day.

Write a solution to find all dates' Id with higher temperatures compared to its previous dates (yesterday).

Return the result table in any order.

The result format is in the following example.

Input:
Weather table:

id recordDate temperature
1 2015-01-01 10
2 2015-01-02 25
3 2015-01-03 20
4 2015-01-04 30

Output:

id
2
4

Explanation:
In 2015-01-02, the temperature was higher than the previous day (10 -> 25).
In 2015-01-04, the temperature was higher than the previous day (20 -> 30).

解答:

SELECT w1.id FROM Weather w1, Weather w2 
WHERE DATEDIFF(w1.recordDate,w2.recordDate) = 1 
AND w1.temperature > w2.temperature

學習點:
1.自己跟自己比較時SELECT自己兩次。
2.DATEDIFF ( datepart , startdate , enddate ) datepart不傳時返回兩個日期之間的差異。


#SQL







Related Posts

《Designing Web APIs》ch2 API Paradigms

《Designing Web APIs》ch2 API Paradigms

腳踏實地往前走 — 團隊的可執行事項

腳踏實地往前走 — 團隊的可執行事項

Mongo Atlas & Heroku & mailchimp - Newsletter Project

Mongo Atlas & Heroku & mailchimp - Newsletter Project


Comments